package org.longteng.core.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 公共调用类
 * 公共日期转换
 * @author hymn
 */
public class PUtil {
    private static Logger log = LoggerFactory.getLogger(PUtil.class);
    /**默认处理的日期格式yyyy-MM-dd HH:mm:ss*/
    private static String defaultDateformatPattern = "yyyy-MM-dd HH:mm:ss";
    private static String UUIdDateformatPattern = "yyyyMMddHHmmssS";

    /**
     * 转换成日期格式串, 时间序列
     * @return
     */
    public static String dateTimeUUId() {
        SimpleDateFormat dateformatPattern = new SimpleDateFormat(UUIdDateformatPattern);
        return dateformatPattern.format(new Date());
    }

    /**
     * 转换成日期格式串, 默认格式：yyyy-MM-dd HH:mm:ss
     * @param dateTimeValue
     * @return
     */
    public static String defaultDateformat(Date dateTimeValue) {
        SimpleDateFormat dateformatPattern = new SimpleDateFormat(defaultDateformatPattern);
        return dateformatPattern.format(dateTimeValue);
    }

    /**
     * 字符串转为日期, 默认格式：yyyy-MM-dd HH:mm:ss
     * @param dateStr
     * @return
     * @throws ParseException
     */
    public static Date defaultFormatParseDate(String dateStr) throws ParseException {
        SimpleDateFormat dateformatPattern = new SimpleDateFormat(defaultDateformatPattern);
        return dateformatPattern.parse(dateStr);
    }

    /**
     * 字符串转为日期,不抛异常，有异常返回 null, 默认格式：yyyy-MM-dd HH:mm:ss
     * @param dateStr
     * @return
     */
    public static Date defaultFormatParseDateNoThr(String dateStr)  {
        SimpleDateFormat dateformatPattern = new SimpleDateFormat(defaultDateformatPattern);
        Date retDate = null;
        try {
            retDate = dateformatPattern.parse(dateStr);
        }catch (ParseException e) {
            log.error("字符串转为日期异常{}", dateStr,e);
        }
        return  retDate;
    }

    /**
     * 字符串转为日期,不抛异常，有异常返回 默认值, 默认格式：yyyy-MM-dd HH:mm:ss
     * @param dateStr
     * @param defaultValue
     * @return
     */
    public static Date defaultFormatParseDateNoThr(String dateStr, Date defaultValue)  {
        Date retDate = defaultFormatParseDateNoThr(dateStr);
        if (retDate == null) {
            retDate = defaultValue;
        }
        return  retDate;
    }

    /**
     * 转换成日期格式串
     * @param dateTimeMillis
     * @return
     */
    public static String defaultDateformat(long dateTimeMillis) {
        SimpleDateFormat dateformatPattern = new SimpleDateFormat(defaultDateformatPattern);
        return dateformatPattern.format(new Date(dateTimeMillis));
    }

    /**
     *  定义日志字色
     * @param color
     * @param fontType
     * @param content
     * @return
     */
    public static String getColoredStr(int color,int fontType,String content){
        return String.format("\033[%d;%dm%s\033[0m",color,fontType,content);
    }
}
